package com.miplus.ccrm.core.service.commission;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.core.controller.vo.commission.*;
import com.miplus.ccrm.core.pojo.FiCommission;
import com.miplus.ccrm.core.pojo.FiCommissionAgent;
import com.miplus.ccrm.core.service.commission.FiCommissionAgentService;
import com.miplus.ccrm.core.mapper.FiCommissionAgentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author wangzl
* @description 针对表【fi_commission_agent】的数据库操作Service实现
* @createDate 2025-03-03 09:51:50
*/
@Service
public class FiCommissionAgentServiceImpl extends MPJBaseServiceImpl<FiCommissionAgentMapper, FiCommissionAgent>
    implements FiCommissionAgentService{

    @Override
    public IPage<AgentCommissionResVo> getAgentCommissionList(AgentCommissionListReqVo para)
    {
        return selectJoinListPage(
                new Page<>(para.getPageNum(),para.getPageSize()),
                AgentCommissionResVo.class,
                new MPJLambdaWrapper<FiCommissionAgent>()
                        .eq(StringUtils.isNotEmpty(para.getDisttag()),FiCommission::getDisttag,para.getDisttag())
                        .le(StringUtils.isNotEmpty(para.getStartDate()),FiCommission::getStartdate,para.getStartDate())
                        .ge(StringUtils.isNotEmpty(para.getEndDate()),FiCommission::getStartdate,para.getEndDate())
                        .selectAll(FiCommissionAgent.class)
                        .selectAs("fun_getnames('SORTING.QUEUE',t.disttag)",AgentCommissionResVo::getDisttagDesc)
        );
    }
    @Override
    public CmdResVo addAgentCommission(String usrId, FiCommissionAgent fiCommissionAgent)
    {
        fiCommissionAgent.setId(null);
        fiCommissionAgent.setCrdt(new Date());
        fiCommissionAgent.setCrusr(usrId);
        save(fiCommissionAgent);
        return new CmdResVo(1, fiCommissionAgent.getId().toString());
    }
    @Override
    public CmdResVo updateAgentCommission(String usrId, AgentCommissionEditVo para)
    {
        if( ObjectUtils.isNull(para.getId()))
            return new CmdResVo(-1, "没有指定更新的ID");
        FiCommissionAgent fiCommissionAgent = new FiCommissionAgent();
        BeanUtils.copyProperties(para,fiCommissionAgent);
        fiCommissionAgent.setMdusr(usrId);
        fiCommissionAgent.setMddt(new Date());
        updateById(fiCommissionAgent);
        return new CmdResVo(1, "更新成功");
    }
    @Override
    public CmdResVo deleteAgentCommission(Integer id)
    {
        removeById(id);
        return new CmdResVo(1, "删除成功");
    }

    @Override
    public FiCommissionAgent getAgentCommissionByMon(AgentCommissionReqVo para)
    {
        List<FiCommissionAgent> fiCommissions =  baseMapper.selectList( new QueryWrapper<FiCommissionAgent>()
                .eq("disttag",para.getDisttag())
                .le("startdate",para.getStartDate())
                .orderByDesc("startdate")
        );
        if(fiCommissions != null && fiCommissions.size()>0)
            return fiCommissions.get(0);
        else
            return null;
    }
}




